<?php
! defined ( "ROOT_PATH" ) && exit ( 'No direct script access allowed' );
/**
 * weshuo microblog platform
 * @copyright  (c) 2010-2011 weshuo team http://www.weshuo.org
 * @license  Mozilla Public License (MPL 1.1)
 * @author iceweb
 */
class BaseFormCheck
{
	public $int = 1; //整数
	public $string = 2; //字符
	public $intMax = PHP_INT_MAX;
	private $value = '';
	private $message = '';
	
	/**
	 * 检测字段长度和其他属性
	 * @param string $name
	 * @param string $post
	 * @param string $msg
	 * @param array $checkLen
	 * @param array $oterhFun/false
	 */
	public static function formField($name, $post, $msg, $checkLen, $oterhFun = false)
	{
		$value="";
		if (! is_array ( $post ))
		{
			$post = strtoupper ( $post );
		}
		if (! is_array ( $checkLen ))
		{
			throw new Exception ( "checklen参数传递不正确" );
		}
		switch ($post)
		{
			case "POST" :
				$value = isset ( $_POST [$name] ) ? $_POST [$name] : '';
				if (empty ($value ))
				{
					return $msg;
				}
				if (self::checkValueLen ( $value, $checkLen ) !== TRUE)
				{ 
					return self::checkValueLen ( $value, $checkLen );
				}
				if (self::checkFormat ( $value, $oterhFun )!== TRUE)
				{
					return self::checkFormat ( $value, $oterhFun );
				}
				break;
			case "GET" :
				$value = isset ( $_GET [$name] ) ? $_GET [$name] : '';
				if (empty ($value ))
				{
					return $msg;
				}
				if (self::checkValueLen ( $value, $checkLen ) !== TRUE)
				{ 
					return self::checkValueLen ( $value, $checkLen );
				}
				if (self::checkFormat ( $value, $oterhFun )!== TRUE)
				{
					return self::checkFormat ( $value, $oterhFun );
				}
				break;
			case "POST#" :
				$value = isset ( $_POST [$name] ) ? $_POST [$name] : '';
				if (empty ($value ))
				{
					return;
				}
				if (self::checkValueLen ( $value, $checkLen ) !== TRUE)
				{ 
					return self::checkValueLen ( $value, $checkLen );
				}
				if (self::checkFormat ( $value, $oterhFun )!== TRUE)
				{
					return self::checkFormat ( $value, $oterhFun );
				}
				break;
			case "GET#" :
				$this->value = isset ( $_GET [$name] ) ? $_GET [$name] : '';
				if ($ajax)
				{
					$this->message = $msg;
				} else
				{
					throw new Exception ( $msg );
				}
				break;
			default :
				throw new Exception ( "post参数错误" );
				break;
		}
	}
	
	private static function checkValueLen($value, $checkLen)
	{
		
		$valueType = $checkLen [0];
		$valueMixLen = $checkLen [1];
		$valueMaxLen = $checkLen [2];
		$valueErrorMessage = $checkLen [3];
		
		switch ($valueType)
		{
			case 1:
				
				if(!is_numeric($value)){
					return $valueErrorMessage;
				}
				if ($value > $valueMaxLen || $value < $valueMixLen)
				{
					return $valueErrorMessage;
				} else
				{
					return TRUE;
				}
				break;
			case 2 :
				if (strlen ( $value ) > $valueMaxLen || strlen ( $value ) < $valueMixLen)
				{
					return $valueErrorMessage;
				} else
				{
					return TRUE;
				}
				break;
			case 3 :
				if(!is_array($value)){
					return $valueErrorMessage;
				}
				
				$value=json_encode($value);
				if (strlen ( $value ) > $valueMaxLen || strlen ( $value ) < $valueMixLen)
				{
					return $valueErrorMessage;
				} else
				{
					return TRUE;
				}
				break;
			default :
				break;
		}
	}
	
	private static function checkFormat($value, $oterhFun)
	{
		if ($oterhFun)
		{
			if (! is_array ( $oterhFun ))
			{
				throw new Exception ( "参数otherFun格式错误" );
			}
			require_once SOURCE_EXT_PATH.'library/FormatCheckLib.class.php';
			$fun = $oterhFun [0];
			$errorMessage = $oterhFun [1];
			if (FormatCheckLib::$fun($value)){
				return TRUE;
			}
			return $errorMessage;
		}
		return TRUE;
	
	}
}